package com.yangff.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;



import com.yangff.anno.LogExecuteTime;
import com.yangff.entity.admin.entity.Admin;
import com.yangff.entity.admin.entity.dto.AdminDTO;

import com.yangff.entity.admin.entity.vo.AdminVo;
import com.yangff.entity.dto.AccountDTO;
import com.yangff.entity.vo.JwtVo;
import com.yangff.entity.vo.ResultVo;
import com.yangff.service.IAdminService;
import com.yangff.utils.MathUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.security.NoSuchAlgorithmException;

/**
 * <p>
 * 存储管理员的基本信息 前端控制器
 * </p>
 *
 * @author yangff
 * @since 2025-01-15
 */
@LogExecuteTime
@RestController
@RequestMapping("/admin")
@RequiredArgsConstructor
public class AdminController {


    private final IAdminService adminService;

    @PostMapping("/login")
    public JwtVo login(@RequestBody AccountDTO accountDTO) throws Exception {
        return adminService.login(accountDTO);
    }


    //增

    @CachePut(cacheNames = "admin", key = "#admin.account")
    @PostMapping("/register")
    public AdminVo saveAdmin(@RequestBody Admin admin) throws NoSuchAlgorithmException {
        if(admin.getPassword() != null) admin.setPassword(MathUtil.hashWithSHA256(admin.getPassword()));
        adminService.save(admin);
        return BeanUtil.copyProperties(admin, AdminVo.class);
    }


    //按照账户查询管理员信息

    @Cacheable(cacheNames = "admin",key = "#account")
    @GetMapping("/{account}")
    public AdminVo getAdminByAccount(@PathVariable String account)
    {
        Admin admin = adminService.getOne(new LambdaQueryWrapper<Admin>().eq(Admin::getAccount,account));
        AdminVo adminVo = BeanUtil.copyProperties(admin, AdminVo.class);
        return adminVo;
    }

    //按照账号删

    @CacheEvict(cacheNames = "admin", key = "#account")
    @DeleteMapping("/{account}")
    public ResultVo deleteAdminByAccount(@PathVariable String account)
    {
        adminService.remove(new LambdaQueryWrapper<Admin>().eq(Admin::getAccount,account));
        return ResultVo.success();
    }


    //修改
    @CachePut(cacheNames = "admin", key = "#adminDTO.account")
    @PutMapping
    public AdminVo updateAdminByAccount(@RequestBody AdminDTO adminDTO) throws NoSuchAlgorithmException {
        Admin admin = adminService.updateAdminByAccount(adminDTO);
        adminService.update(admin, new LambdaUpdateWrapper<Admin>().eq(Admin::getAccount, adminDTO.getAccount()));
        return BeanUtil.copyProperties(admin, AdminVo.class);
    }


    @PutMapping("/resetPassword")
    public ResultVo updateAdminPassword(@RequestBody AdminDTO adminDTO) throws NoSuchAlgorithmException {
        LambdaUpdateWrapper<Admin> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Admin::getAccount, adminDTO.getAccount())
                .set(Admin::getPassword, MathUtil.hashWithSHA256(adminDTO.getPassword()));
        adminService.update(updateWrapper);
        return ResultVo.success();
    }


}
